package com.gxks.lhs.controller.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.Favorite;
import com.gxks.lhs.entity.Goods;
import com.gxks.lhs.security.SecurityUtils;
import com.gxks.lhs.service.IFavoriteService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-19
 */
@RestController
@RequestMapping("/restful-api/favorite")
public class FavoriteController {

    @Autowired
    private IFavoriteService favoriteService;

    @ApiOperation("添加或删除收藏")
    @PostMapping("/addOrCancel/{goodsId}")
    public R addFavorite(@PathVariable String goodsId) {
        System.out.println("收藏的商品id" + goodsId);
        String userId = SecurityUtils.getCurrentUserId();
        if (favoriteService.addOrCancelFavorite(userId, goodsId)) {
            return R.success();
        }
        return R.error("执行失败");
    }

    @ApiOperation("查询当前商品是否是用户的收藏")
    @GetMapping("/isFavorite")
    public R isFavorite(@RequestParam String goodsId) {
        System.out.println("收藏的商品id" + goodsId);
        String userId = SecurityUtils.getCurrentUserId();
        if (favoriteService.isFavorite(userId, goodsId)) {
            return R.success(1, "已收藏");
        } else {
            return R.success(0, "未收藏");
        }
    }


    @ApiOperation("查询所有收藏")
    @GetMapping("/queryList")
    public R queryFavorite(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                           @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        String id = SecurityUtils.getCurrentUserId();
        Page<Goods> favoriteList = favoriteService.getFavoriteList(id, current, size);
        if (favoriteList != null) {
            return R.success(favoriteList, "查询成功");
        }
        return R.error("查询失败");
    }


    @ApiOperation("根据id列表批量删除收藏")
    @DeleteMapping("/deleteList")
    public R deleteFavoriteBatch(List ids) {
        if (favoriteService.deleteFavoriteByIds(ids)) {
            return R.success().setMsg("删除成功");
        }
        return R.error("删除失败");
    }

}

